package com.situ.mall.common.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class RequestUtil {

    private static final String CURRENT_USER = "currentUser";

    //获取当前请求
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    public static void setCurrent(Object u) {
        getSession().setAttribute(CURRENT_USER, u);
    }

    public static Object getCurrent() {
        return getSession().getAttribute(CURRENT_USER);
    }

    public static void logout() {
        getSession().removeAttribute(CURRENT_USER);
    }
}
